package com.ks.winterwarmimage.adapter;



import android.graphics.PorterDuff;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.IntDef;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.ks.winterwarmimage.R;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.List;

public class DeviceRecyclerAdapter extends RecyclerView.Adapter<DeviceRecyclerAdapter.ViewHolder> {
    private List<DevItem> devItemList=null;
    @IntDef({ClickId.CLICK_ITEM,ClickId.CLICK_MENU})
    @Retention(RetentionPolicy.SOURCE)
    /**MQTT连接状态枚举*/
    public @interface ClickId {
        /**项目单击*/
        int CLICK_ITEM=0;
        /**菜单按钮单击（弹出菜单里面包含）*/
        int CLICK_MENU=1;
    }
    /**列表项目单击事件监听器*/
    public interface OnItemClickListener {
        /**
         * 列表项目单击事件
         * @param view 单击的项目视图
         * @param position 项目位置
         */
        void onItemClick(View view, int position,  @ClickId int clickId);
    }
    /**列表项目单击事件监听器*/
    private OnItemClickListener mListener;
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
    /**
     * 构造函数
     * @param devItemList 设备列表
     */
    public DeviceRecyclerAdapter(List<DevItem> devItemList) {
        this.devItemList = devItemList;
    }
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater mInflater = LayoutInflater.from(parent.getContext());
        View view = mInflater.inflate(R.layout.device_view, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        DevItem item=devItemList.get(position);
        holder.setDevice(item,position,view ->  {
            if(this.mListener!=null){
                this.mListener.onItemClick(view,holder.position,view==holder.itemView?ClickId.CLICK_ITEM:ClickId.CLICK_MENU);
            }
        });
    }

    @Override
    public int getItemCount() {
        return devItemList==null?0:devItemList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        private ImageView image,imgResetNetwork;
        private TextView textName,textState,textTemp;
        private TextView textRelayState;
        /**当前位置*/
        public int position;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            image=itemView.findViewById(R.id.imageView);
            imgResetNetwork=itemView.findViewById(R.id.img_reset_network);
            textName=itemView.findViewById(R.id.text_name);
            textState=itemView.findViewById(R.id.text_state);
            textTemp=itemView.findViewById(R.id.text_temp);
            textRelayState=itemView.findViewById(R.id.text_relay_state);
            textRelayState.setText(R.string.off);   //关闭开关
        }
        /**
         * 设置设备信息
         * @param item
         * @param position
         * @param listener
         */
        public void setDevice(DevItem item,int position,View.OnClickListener listener){
            textName.setText(item.devName);
            textName.setTag(item.deviceId);
            textTemp.setText(String.format("%.1f℃", item.temp));
            textRelayState.setText(item.relayState==1&&item.devStatus==1?R.string.on:R.string.off);
            textRelayState.setSelected(item.relayState==1&&item.devStatus==1);
            ViewGroup  parent=(ViewGroup)textRelayState.getParent();
            if(item.devStatus==0){   //离线
                textState.setText(R.string.offline);
                textState.setTextColor(0xFFBABABA);
                textTemp.setTextColor(0xFFBABABA);
            }else{  //在线
                textState.setText(R.string.online);
                textState.setTextColor(0xFF00BCD4);
                textTemp.setTextColor(0xFFFFA68B);
            }
            this.position = position;
            imgResetNetwork.setImageResource(item.devStatus==0?R.drawable.ic_network_24:R.drawable.ic_wifi_24dp);
            imgResetNetwork.setColorFilter(item.devStatus==0?0xFFBABABA:0xFF00BCD4, PorterDuff.Mode.SRC_IN);
            itemView.setOnClickListener(listener);
            imgResetNetwork.setOnClickListener(listener);
        }
    }
}
